import HeroControl, {animName, colorType} from "./HeroControl";
import GameData_red_blue_man, {emitType} from "./GameData_red_blue_man";
import KeyControl_red_blue_man from "./KeyControl_red_blue_man";
import GameControl_red_blue_man from "./GameControl_red_blue_man";
import SpineControl from "./SpineControl";
import Music, {musicName} from "../PublicScript/Music";
import Money from "./Money";

const {ccclass, property} = cc._decorator;

export enum colliderGroup{
    default = "default_red",
    mirror = "mirror_red",
    box = "box_red",
    iron = "iron_red",
    key = "key_red",
    switch = "switch_red",
    hero = "hero_red",
    fluid = "fluid_red",
    bridge = "bridge_red",
    spine = "spine_red",
    seesaw = "seesaw_red",
    mirrorEndPoint = "mirrorEndPoint_red",
    coin = "coin_red",
    springBox = "springBox_red"
}

@ccclass
export default class HeroContact extends HeroControl{

    start () {
        super.init();
    }

    //碰撞到墙壁
    wall_dir = 0;
    faceStone = false;

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact, self, other) {
        if(other.node.group == colliderGroup.springBox){
            return;
        }
        if(other.node.group == colliderGroup.default && other.tag == 99 && self.tag == 0){
            console.log(GameControl_red_blue_man.heroState[this.manType], "===============================");
            GameControl_red_blue_man.heroState[this.manType] = true;
            if(GameControl_red_blue_man.gameState){
                console.log("游戏结束");
                cc.game.emit(emitType.gameOver);
            }
        }
        if(other.node.group == colliderGroup.default && other.tag == 98 && self.tag == 0){
            console.log(GameControl_red_blue_man.heroState[this.manType]);
            GameControl_red_blue_man.heroState[this.manType] = false;
        }
        if(other.node.group == colliderGroup.spine){
            if(other.tag == 0){
                other.node.active = false;
            }
            if( other.node.parent.getComponent(SpineControl)){
                other.node.parent.getComponent(SpineControl).remove();
            }
            this.die();
        }
        if(other.node.group == colliderGroup.fluid){
            switch (other.tag) {
                case 1:         //碰到火
                    if(this.manType == colorType.blue){
                        this.die();
                    }else{
                        this.stopRunEffectAnim();
                    }
                    break;
                case 2:         //碰到水
                    if(this.manType == colorType.red){
                        this.die();
                    }else{
                        this.stopRunEffectAnim();
                    }
                    break;
                case 3:         //碰到毒
                    this.die();
                    break;
                case 4:
                    // this.isContact = true;
                    this.judgeJump();
                    break;
            }
        }

        //判断脚踩到铁桥
        if(self.tag == 1 && this.jumpLock && (other.node.group == colliderGroup.bridge || other.node.group == colliderGroup.seesaw)){
            if(other.node.group == colliderGroup.bridge){
                Music.GamePlayEffect(musicName.bridge_sound);
            }
            this.isBridge = true;
            this.judgeJump();
            this.jumpCountIndex = 0;
            this.jumpLock = false;
        }

        if(self.tag == 1 && this.jumpLock  && (other.node.group == colliderGroup.default || other.node.group == colliderGroup.box || other.node.group == colliderGroup.switch
            || (other.node.group == colliderGroup.mirror && other.tag == 99))){      //主要用来检查是否脚碰底
            this.judgeJump();
        }
        //捡钥匙
        if(self.tag == 0 && other.node.group == colliderGroup.key){
            let script = other.node.getComponent(KeyControl_red_blue_man);
            if(script.keyType == this.manType){
                Music.GamePlayEffect(musicName.key_sound);
                this.scheduleOnce(()=>{
                    cc.game.emit(emitType.getKey, other.node, script.keyType);
                },0.1);
            }
        }
        //面朝墙壁推石头
        if(self.tag == 2 && other.node.group == colliderGroup.default && other.node.name == "l1"){
            if(this.wall_dir != 0){
                return;
            }
            //判断墙是在左边还是右边
            //获取碰撞的法向量
            if(this.onMoveSide){
                return;
            }
            let normal = contact.getWorldManifold().normal;
            if(normal.x < 0){
                this.wall_dir = 1;
            }else if(normal.x > 0){
                this.wall_dir = -1;
            }
            // console.log("this.wall_dir:",this.wall_dir);
        }
        //是否接触到石头
        if(other.node.group == colliderGroup.box && self.tag == 2){
            // console.log("接触");
            this.faceStone = true;
            this.pushStoneAnim = true;
        }
    }

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact(contact, self, other) {
        if(GameControl_red_blue_man.gameState){
            return;
        }
        if(self.tag == 2 && other.node.name == "l1"){
            // console.log("离开");
            this.wall_dir = 0;
        }
        //判断脚踩到铁桥
        if(self.tag == 1 && (other.node.group == colliderGroup.bridge || other.node.group == colliderGroup.seesaw)){
            this.isBridge = false;
        }
        //是否离开接触石头
        if(other.node.group == colliderGroup.box && self.tag == 2){
            // console.log("离开石头，");
            this.faceStone = false;
            this.pushStoneAnim = false;
            Music.stopSpecialMusic({name: musicName.stone_Sound});
        }

        if(other.node.group == colliderGroup.box && this.wall_dir != 0){
            this.pushStoneAnim = false;
            Music.stopSpecialMusic({name: musicName.stone_Sound});
        }

        if(self.tag == 2 && this.wall_dir == 0 && other.node.group == colliderGroup.default && !this.faceStone){
            this.pushStoneAnim = false;
            Music.stopSpecialMusic({name: musicName.stone_Sound});
        }
    }

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve(contact, self, other) {
        if(GameControl_red_blue_man.gameState){
            return;
        }
        if(other.node.group == colliderGroup.box && this.wall_dir != 0 && this.move_lock){
            this.pushStoneAnim = true;
            other.body.linearVelocity = cc.v2(130 * this.wall_dir,other.body.linearVelocity.y);
        }
    }

    // update (dt) {}
}
